<template>
  <el-dialog append-to-body :title="title" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false" :destroy-on-close="true" :visible.sync="visible" :width="'480px'">
    <el-form :model="myForm" label-width="100px" size="small" @submit.native.prevent>
      <el-row :gutter="10">
        <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
          <el-form-item label="部门名称" required>
            <el-input size="small" v-model.trim="myForm.name" placeholder="请输入部门名称" maxlength="30" show-word-limit></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
          <el-form-item label="上级部门">
            <el-cascader size="small" placeholder="请选择上级部门" clearable filterable :options="depart" :props="{label: 'name', value: 'id', checkStrictly: true}" :disabled="disabled" v-model="myForm.parent" class="full-width"></el-cascader>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
          <el-form-item label="是否启用" v-if="!noStatus">
            <el-switch v-model="myForm.status"></el-switch>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :disabled="submitLoading" @click="closeModel()">取消</el-button>
      <el-button type="primary" :loading="submitLoading" @click="validForm()">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { AddDepartmentItemData } from '@/views/user-management/services/index';
export default {
  props: ['visible', 'title', 'depart', 'parentNode', 'noStatus'],
  data() {
    return {
      myForm: {
        name: null,
        parent: null,
        status: true,
      },
      disabled: false,
      submitLoading: false
    }
  },
  watch: {
    parentNode: {
      immediate: true,
      handler(newValue) {
        if (!newValue || !newValue.length) {
          this.disabled = false;
          return;
        }
        this.myForm.parent = newValue.map(item => item.id);
        this.disabled = true;
      }
    }
  },
  methods: {
    closeModel() {
      this.$emit('update:visible', false);
      this.resetForm();
    },
    validForm() {
      if (!this.myForm.name) {
        this.$message.warning('请输入部门名称!');
        return;
      }
      this.submitLoading = true;
      AddDepartmentItemData({
        proId: 4,
        name: this.myForm.name,
        isEnable: this.myForm.status ? 1 : 0,
        parentId: this.myForm.parent && this.myForm.parent.length ? this.myForm.parent[this.myForm.parent.length - 1] : '0'
      })
      .then(success => {
        this.submitLoading = false;
        if (success.code == 200) {
          this.$message.success(`${this.title}成功!`);
          this.closeModel();
          this.$emit('update', success.data);
        } else {
          this.$message.error(`${this.title}失败!`);
        }
      })
      .catch(error => {
        this.submitLoading = false;
        this.$message.error(error);
      })
    },
    resetForm() {
      this.submitLoading = false;
      this.myForm.name = null;
      this.myForm.parent = null;
      this.myForm.status = true;
    }
  }
}
</script>
